package com.qianxing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianxing.dto.AdminLoginDTO;
import com.qianxing.dto.NationUploadDataDTO;
import com.qianxing.dto.RegionUpLoadDataDTO;
import com.qianxing.exceptionhandler.ResponseException;
import com.qianxing.mapper.TbNationAreaLanguagePackMapper;
import com.qianxing.mapper.TbNationLanguagePackMapper;
import com.qianxing.mapper.WxAdminMapper;
import com.qianxing.po.AdminLoginPO;
import com.qianxing.po.TbNationAreaLanguagePackPO;
import com.qianxing.po.TbNationLanguagePackPO;
import com.qianxing.service.WxAdminService;
import com.qianxing.utils.MultipartFileToFileUtils;
import com.qianxing.utils.TencentCOSFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Author:JasonLong
 * Date:2023/5/12
 * Time:22:50
 * 管理员业务接口实现类
 */
@Service
public class WxAdminServiceImpl implements WxAdminService {

    @Autowired
    private WxAdminMapper wxAdminMapper;

    @Autowired
    private TbNationAreaLanguagePackMapper wxRegionalDialectMapper;

    @Autowired
    private TbNationLanguagePackMapper wxNationDialectMapper;

    @Override
    public ResponseException adminRegister(AdminLoginDTO adminLoginDTO) {
        //判断是否已存在
        QueryWrapper<AdminLoginPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_account", adminLoginDTO.getAccount()).eq("deleted", 0);
        List<AdminLoginPO> adminLoginPOList = wxAdminMapper.selectList(queryWrapper);
        boolean emptyJudge = adminLoginPOList.isEmpty();
        if (emptyJudge) {
            AdminLoginPO adminLoginPO = new AdminLoginPO();
            adminLoginPO.setAccount(adminLoginDTO.getAccount());
            adminLoginPO.setPassword(adminLoginDTO.getPassword());
            int insert = wxAdminMapper.insert(adminLoginPO);
            System.out.println(insert > 0 ? "insert_yes" : "insert_no");
            return ResponseException.success("200", "账号注册成功！");
        } else {
            return ResponseException.error("500", "该账号已存在，请直接登录！");
        }
    }

    @Override
    public ResponseException adminLogin(AdminLoginDTO adminLoginDTO) {
        String account = adminLoginDTO.getAccount();
        String password = adminLoginDTO.getPassword();
        Map map = new HashMap<>();
        map.put("admin_account", account);
        map.put("admin_password", password);
        List list = wxAdminMapper.selectByMap(map);
        if (list.isEmpty()) {
            return ResponseException.error("500", "账号或者密码错误，请核对后重试!");
        } else {
            return ResponseException.success("200", "登陆成功!");
        }
    }

    @Value("${upload.regionVoiceUrl}")
    private String regionServerUrl;

    @Value("${upload.nationVoiceUrl}")
    private String nationServerUrl;

    @Override
    public ResponseException adminUploadRegion(RegionUpLoadDataDTO regionUpLoadDataDTO) {
        MultipartFile multipartFile = regionUpLoadDataDTO.getMultipartFile();
        //原始文件名
        String originalFilename = multipartFile.getOriginalFilename();
        //文件后缀名(包括.)
        String substring = originalFilename.substring(originalFilename.indexOf("."), originalFilename.length());
        //UUID
        UUID uuid = UUID.randomUUID();
        String newFileName = uuid + substring;
        String nationAreaFlag = regionUpLoadDataDTO.getNationAreaFlag();
        String nationAreaTranslation = regionUpLoadDataDTO.getNationAreaTranslation();
        //利用工具类将MultipartFile转为File,存储桶要求必须是File类型
        File file = MultipartFileToFileUtils.multipartFileToFile(multipartFile);
        //存储文件到本地目录（线上时也是在同样位置配置为存储到服务器即可）
        //COS对象存储返回访问地址
        String nationAreaVoiceurl = TencentCOSFileUpload.uploadfile(file, newFileName, regionServerUrl);
        TbNationAreaLanguagePackPO regionalDataPO = new TbNationAreaLanguagePackPO();
        regionalDataPO.setNationAreaFilename(newFileName);
        regionalDataPO.setNationAreaVoiceurl(nationAreaVoiceurl);
        regionalDataPO.setNationAreaTranslation(nationAreaTranslation);
        regionalDataPO.setNationAreaFlag(nationAreaFlag);
        int insert = wxRegionalDialectMapper.insert(regionalDataPO);
        if (insert > 0) {
            return ResponseException.success("200", "上传成功！");
        } else {
            return ResponseException.error("500", "上传失败！");
        }
    }

    @Override
    public ResponseException adminUploadNation(NationUploadDataDTO nationUploadDataDTO) {

        MultipartFile multipartFile = nationUploadDataDTO.getMultipartFile();
        //原始文件名
        String originalFilename = multipartFile.getOriginalFilename();
        //文件后缀名(包括.)
        String substring = originalFilename.substring(originalFilename.indexOf("."), originalFilename.length());
        //UUID
        UUID uuid = UUID.randomUUID();
        String newFileName = uuid + substring;
        String nationFlag = nationUploadDataDTO.getNationFlag();
        String nationTranslation = nationUploadDataDTO.getNationTranslation();
        //利用工具类将MultipartFile转为File,这里暂时用不到，但是用到存储桶的时候要求必须是File类型
        File file = MultipartFileToFileUtils.multipartFileToFile(multipartFile);
        //存储文件到本地目录（线上时也是在同样位置配置为存储到服务器即可）
//        String nationVoiceurl = UploadToLocalUtils.regionFileUpload(multipartFile,newFileName);
        //对象存储调用成功后返回一个访问地址
        String nationVoiceurl = TencentCOSFileUpload.uploadfile(file, newFileName, nationServerUrl);
        TbNationLanguagePackPO nationDataPO = new TbNationLanguagePackPO();
        nationDataPO.setNationFilename(newFileName);
        nationDataPO.setNationVoiceurl(nationVoiceurl);
        nationDataPO.setNationTranslation(nationTranslation);
        nationDataPO.setNationFlag(nationFlag);
        int insert = wxNationDialectMapper.insert(nationDataPO);
        if (insert > 0) {
            return ResponseException.success("200", "上传成功！");
        } else {
            return ResponseException.error("500", "上传失败！");
        }
    }

    @Override
    public ResponseException adminUpdateRegion() {
        return null;
    }

    @Override
    public ResponseException adminDeleteRegion() {
        return null;
    }

    @Override
    public ResponseException adminUpdateNation() {
        return null;
    }

    @Override
    public ResponseException adminDeleteNation() {
        return null;
    }
}
